package com.group01.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group01.community.entity.*;
import com.group01.community.entity.vo.ZyOwnerRoomVO;
import com.group01.community.entity.vo.ZyOwnerVO;
import com.group01.community.service.*;
import com.group01.community.utils.ResponseResult;
import com.group01.community.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 房屋绑定表  前端控制器
 * </p>
 *
 * @author xuxu
 * @since 2022-07-04
 */
@Api(tags = "房屋审核信息")
@RestController
@RequestMapping("/zyOwnerRoom")
public class ZyOwnerRoomController {

    @Autowired
    private ZyOwnerRoomService zyOwnerRoomService;

    @Autowired
    private ZyRoomService zyRoomService;

    /**
     * 分页查询 + 模糊查询 +按条件查询房屋绑定对象
     *
     * @param currentPage           当前页
     * @param sizePage              每页显示的数据
     * @param communityName         小区名字
     * @param roomStatus            绑定状态
     * @return                      房屋信息对象
     */
    @ApiOperation("房屋绑定列表的分页查询和条件查询")
    @GetMapping("zyOwnerRoomList")
    public ResponseResult<IPage<ZyOwnerRoomVO>> getZyOwnerRoomList(Long currentPage,Long sizePage,String communityName,String roomStatus){
        IPage<ZyOwnerRoomVO> zyOwnerRoomVOIPage = zyOwnerRoomService.listAllOwnerRoom(currentPage, sizePage, communityName, roomStatus);
        return ResponseResult.ok(zyOwnerRoomVOIPage);
    }


    /**
     * 更新房屋审核状态
     *
     * @param zyOwnerRoomVO             房屋审核对象
     * @return                          修改后的状态
     */
    @PutMapping("update")
    public ResponseResult<String> updateRoomStatus(@RequestBody ZyOwnerRoomVO zyOwnerRoomVO){
        zyOwnerRoomService.updateRoomStatusByZyOwnerRoomVO(zyOwnerRoomVO);
        return ResponseResult.ok("ok");
    }


    /**
     * 解绑信息
     *
     * @param ownerRoomId        解绑房屋Id
     * @return                  ok
     */
    @DeleteMapping("unbundled/{ownerRoomId}")
    public ResponseResult<String> updateRoom(@PathVariable Long ownerRoomId){
        zyOwnerRoomService.updateRoomStatusByZyOwner(ownerRoomId);
        return ResponseResult.ok("ok");
    }

    /**
     * 添加房屋绑定
     * @param zyOwnerRoom       房屋对象
     * @return                  ok
     */
    @PostMapping("addRoom")
    public ResponseResult<String> addRoom(ZyOwnerRoom zyOwnerRoom){
        if (zyOwnerRoomService.getOwnerRoomByRoom(zyOwnerRoom) == null){
            zyOwnerRoom.setOwnerRoomId(UUIDUtils.getId());
            zyOwnerRoom.setOwnerType("yz");
            zyOwnerRoom.setRoomStatus("Auditing");
            zyOwnerRoom.setCreateTime(LocalDateTime.now());
            zyOwnerRoomService.save(zyOwnerRoom);
            return ResponseResult.ok("ok");
        }
        return ResponseResult.ok("bad");
    }

    /**
     * 通过单元Id获取所有的房间号
     * @param node        单元Id
     * @return              房间的集合
     */
    @GetMapping("getAllRoomByUnitId/{node}")
    public ResponseResult<List<ZyRoom>> getAllRoomByUnitId(@PathVariable Long node){
        List<ZyRoom> list = zyRoomService.list(new QueryWrapper<ZyRoom>().eq("unit_id", node));
        return ResponseResult.ok(list);
    }
}

